package com.ruoyi.web.wapper;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.wapper.BaseControllerWrapper;
import com.ruoyi.extend.enums.ChannelTypeEnum;
import com.ruoyi.product.constant.ClientTypeConstant;

import java.util.*;

public class ProductPushWapper extends BaseControllerWrapper {

    public ProductPushWapper(Map<String, Object> single) {
        super(single);
    }

    public ProductPushWapper(List<Map<String, Object>> multi) {
        super(multi);
    }

    private Map<Long, String> resultMap = new HashMap<>();
    public ProductPushWapper setScencInfoList(List<Map<String, Object>> scencInfoList) {

        for (Map<String, Object> map: scencInfoList){
            Long value = (Long) map.get("value");
            String name = (String) map.get("name");
            String label = (String) map.get("label");
            resultMap.put(value, label);
//            resultMap.put(value, name);
        }
        return this;
    }

    @Override
    public void wrapTheMap(Map<String, Object> map) {

        try {
            String scencIds = (String)map.get("sceneIds");
            if (StringUtils.isNotEmpty(scencIds)){
                 List<Long> idList = JSON.parseArray(scencIds, Long.class);
                 List<String> resultList = new ArrayList<>();
                 for (Long id: idList){
                     if (resultMap.containsKey(id)){
                         resultList.add(resultMap.get(id));
                     }
                 }
                 map.put("sceneIds", JSON.toJSONString(resultList));
            }

            String clientTypes = (String)map.get("clientTypes");
            if (StringUtils.isNotEmpty(clientTypes)){
                List<String> typeList = JSON.parseArray(clientTypes, String.class);
                List<String> resultList = new ArrayList<>();
                for (String type: typeList){
                    if (ClientTypeConstant.typeMap.containsKey(type)){
                        resultList.add(ClientTypeConstant.typeMap.get(type));
                    }
                }
                map.put("clientTypes", JSON.toJSONString(resultList));
            }

            String channelTypes = (String)map.get("channelTypes");
            if (StringUtils.isNotEmpty(channelTypes)){
                List<String> typeList = JSON.parseArray(channelTypes, String.class);
                List<String> resultList = new ArrayList<>();
                for (String type: typeList){
                    resultList.add(ChannelTypeEnum.getNameByValue(type));
                }
                map.put("channelTypes", JSON.toJSONString(resultList));
            }


//            String notShowCitys = (String)map.get("notShowCitys");
//            if (StringUtils.isNotEmpty(notShowCitys)){
//                JSONArray jsonArray = JSON.parseArray(notShowCitys);
//
//                String newContent = "";
//                for (int index = 0; index < jsonArray.size(); index++){
//                    JSONObject jsonObject = jsonArray.getJSONObject(index);
//                    String chCode = jsonObject.getString("chCode");
//
//                    JSONArray cityArrArray = jsonObject.getJSONArray("cityArr");
//                    String cityArr = Arrays.toString(cityArrArray.stream().toArray());
//                    newContent += chCode + ":" + cityArr + "|";
//                }
//                map.put("notShowCitys", newContent);
//            }


        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
